var Ui = (function( opts ) {
	var defaults = {},
		settings = $.extend(defaults, opts),
		products;

	
	function init() {
		// YT
		// close
		$('.scrollable').scroll( function(e) {
			e.preventDefault();
			//console.log('scrollin');
			return false;
		})
		
		$('.wallpaper-download-link').click( function(e)  {
			e.preventDefault();
			var flyout = $(this).parent().find('.wallpaper-flyout');
			flyout.slideToggle('fast');
		});

		
		// Share bits
		$('.share-link','.social-buttons').click(function(e){
			e.preventDefault();
			openShareLink($(this).attr('href'),$(this).html());
			// call omniture
			s.tl(this,'o','usJ23:stories:AJ2012:social-share-'+$(this).html()+'-'+$(this).attr('rel'))
		})
	    $(".tweet-flyover").tweet({
	        count: 20,
	        loading_text: "loading tweets...",
	        query: "#FlyOver",
	        template: "{user} {join} {text} {time}"
	    });
	    $(".tweet-flyaround").tweet({
	        count: 20,
	        loading_text: "loading tweets...",
	        query: "#FlyAround",
	        template: "{user} {join} {text} {time}"
	    });
	    $(".tweet-flythrough").tweet({
	        count: 20,
	        loading_text: "loading tweets...",
	        query: "#FlyThrough",
	        template: "{user} {join} {text} {time}"
	    });

	    $('#nav-item-1').click(function(e){
	    	s.tl(this,'o','usJ23:stories:AJ2012:FlyOverNav-click');
	    });
	    $('#nav-item-2').click(function(e){
	    	s.tl(this,'o','usJ23:stories:AJ2012:FlyAroundNav-click');
	    });
	    $('#nav-item-3').click(function(e){
	    	s.tl(this,'o','usJ23:stories:AJ2012:FlyThroughNav-click');
	    });
	    $('.wallpaper-link').click(function(e){
	    	s.tl(this,'d','usJ23:stories:AJ2012:'+$(this).attr('rel'));
	    })
	    $('#nav-share').click(function(e){
	    	s.tl(this,'o','usJ23:stories:AJ2012:nav-share-click');
	    });

	    // buynow
	    products = {
			'fly-over': {
				'colorId': '180',
				'styleId': '508318',
				'nikeStoreLink': 'http://store.nike.com/us/en_us/?l=shop,pdp,ctr-inline/cid-1/pid-440398/pgid-461846'
			},
			'fly-around-blue': {
				'colorId': '172',
				'styleId': '508320',
				'nikeStoreLink': 'http://store.nike.com/us/en_us/?l=shop,pdp,ctr-inline/cid-1/pid-440403/pgid-440404'
			},
			'fly-around-red': {
				'colorId': '182',
				'styleId': '508320',
				'nikeStoreLink': 'http://store.nike.com/us/en_us/?l=shop,pdp,ctr-inline/cid-1/pid-440402/pgid-440404'
			},
			'fly-through-blue': {
				'colorId': '181',
				'styleId': '508319',
				'nikeStoreLink': 'http://store.nike.com/us/en_us/?l=shop,pdp,ctr-inline/cid-1/pid-440399/pgid-440401'
			},
			'fly-through-red': {
				'colorId': '171',
				'styleId': '508319',
				'nikeStoreLink': 'http://store.nike.com/us/en_us/?l=shop,pdp,ctr-inline/cid-1/pid-440400/pgid-440401'
			}
		}

	    	JumpmanBuyNow.getRetailers({
			products: products,
				webService: '/jumpman23/services/buynow/prodCheck_xml.jsp?colorId={colorId}&styleId={styleId}',
			//webService: '/site/data/test.xml?colorId={colorId}&styleId={styleId}',
			onLoadComplete: function() {
				products = this;
				buynowUi();
			}
		});

	}  



	function buynowUi() {
		
		$('.colorways a').click(function(e) {
			e.preventDefault();

			// swap out shoe thumb
			var imgId = returnHash(this.href);

			var imgElem = $(imgId);
			imgElem.parent().find('img').hide();

			imgElem.show();

			// change selected 
			$(this).parent().parent().find('a').removeClass('selected');
			$(this).addClass('selected');
		});

		$('.button-available').click(function(e) {
			e.preventDefault();

			var socialSection = $('.social-section.' + returnHash(this.href).substring(1));

			showRetailList(socialSection);

		});

	};

	// buy now pieces
	function showRetailList(socialSection) {
		// find out which colorway within the section is selected.  
		var shoeId = socialSection.find('.colorways .selected').attr('href').substring(1);

		// grab the retails list elem
		var retailList = socialSection.find('ul.retailers');

		// show/hide toggle reatiler list
		if (retailList.hasClass('open')) {
			retailList.removeClass('open').hide();
			return false;
		} else {
			retailList.addClass('open');
		}

		// show retailers
		
		var retailListCount = 0;
		var shoe = products[shoeId];

		retailList.empty();

		socialSection.find('ul.retailers').append('<li><a href="' + shoe.nikeStoreLink + '" target="_blank">Nike Store</a></li>').show();

		var retailers = $(products[shoeId].retailers);

		retailers.find('retailer').each(function(i) {

			var retailer = $(shoe.retailers).find('retailer')[i];
			var retailerName = $(retailer).find('name').text();
			var pdpUrl = $(retailer).find('pdpUrl').text();


			socialSection.find('ul.retailers').append('<li><a href="' + pdpUrl + '" target="_blank">' + translateName(retailerName) + '</a></li>').show();

			retailListCount++;
		});

	}

	function translateName(name) {
		var retailer;
		switch (name) {
		case 'champssports':
			retailer = 'Champs Sports';
			break;
		case 'footlocker':
			retailer = 'Foot Locker';
			break;
		case 'eastbay':
			retailer = 'Eastbay';
			break;
		case 'footaction':
			retailer = 'Footaction';
			break;
		default:
			retailer = name;
		};

		return retailer;
	};

	function returnHash(urlStr) {
		return urlStr.substring(urlStr.indexOf('#'));
	};



	openShareLink = function (url,site) {
		if (site == 'twitter') {
			var h = 230; 
			var w = 480;
		} else {
			var h = 350;
			var w = 600;
		};
		window.open(url,site+'_share','directories=no,menubar=no,status=no,titlebar=no,toolbar=no,resizable=yes,width='+w+',height='+h);
	}


	// ---------------------------------------------
	// PUBLIC
	// ---------------------------------------------
	return {
		init: init
	}
})();


